/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JPanel;

import twaver.Element;
import twaver.Node;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.TaskAdapter;
import twaver.TaskScheduler;
import twaver.chart.LineChart;

public class MemoryChartPane extends JPanel {
    private Element item = new Node();
    private LineChart memoryChart = new LineChart();
    private Runtime runtime = Runtime.getRuntime();

    public MemoryChartPane() {    	
    	item.setName("Memory Usage");
    	item.putChartColor(Color.green.darker());
    	memoryChart.getDataBox().addElement(item);
    	
    	Font font = TUIManager.getDefault10SizeFont();
    	memoryChart.setBackground(Color.BLACK);
    	memoryChart.setForeground(Color.WHITE);
        memoryChart.getTitleLabel().setFont(font);
        memoryChart.setYAxisTextFont(font);
        memoryChart.setLegendFont(font);
    	memoryChart.setLineType(TWaverConst.LINE_TYPE_AREA);
        memoryChart.setYAxisTextColor(Color.white);
        memoryChart.setLegendOrientation(TWaverConst.LABEL_ORIENTATION_RIGHT);
        memoryChart.setLegendLayout(TWaverConst.LEGEND_LAYOUT_VERTICAL);
        memoryChart.setHighlightBackground(new Color(0, 255, 0, 128));
        memoryChart.setHighlightForeground(Color.YELLOW);
        memoryChart.setEnableYTranslate(false);
        memoryChart.setEnableYZoom(false);
        memoryChart.setAntialias(false);
        memoryChart.setLowerLimit(0);
        memoryChart.setBackgroundVisible(true);

        this.setLayout(new BorderLayout());
        this.add(memoryChart, BorderLayout.CENTER);
        this.setPreferredSize(new Dimension(180, 150));
        
        TaskScheduler.getInstance().register(new TaskAdapter() {
            public void run(long clock) {
                long total = runtime.totalMemory();
                memoryChart.setYAxisText("Tot: " + NumberFormat.getInstance().format(total) + " bs");
                memoryChart.setUpperLimit(total);

                long free = runtime.freeMemory();
                memoryChart.setTitle("Used: " + NumberFormat.getInstance().format(free) + " bs");
                item.addChartValue(free);
                if (memoryChart.valueCount() > 100) {
                    memoryChart.removeHead(1);
                }
            }
            public int getInterval() {
                return 1000;
            }
        });
        
        final JButton gc = new JButton("Collect Garbage");
        gc.setMargin(TWaverConst.NONE_INSETS);
        gc.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.gc();
            }
        });
        this.add(gc, BorderLayout.SOUTH);
        
        
    }
}